package com.yin.myproject.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 有四个线程1、2、3、4。
 * <p>
 * 线程1的功能就是输出1，线程2的功能就是输出2，
 * <p>
 * 以此类推......... 现在有四个文件A B C D,
 * 初始都为空。现要让四个文件呈如下格式：
 * A：1 2 3 4 1 2....
 * B：2 3 4 1 2 3....
 * C：3 4 1 2 3 4....
 * D：4 1 2 3 4 1....
 * <p>
 * <p>
 * Created by Eason on 2017/7/25.
 */
public class FileThread implements Runnable {
    private int value;
    private String name;

    public FileThread(int value, String name) {
        this.value = value;
        this.name = name;
    }

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(4);
        executorService.execute(new FileThread(1, "线程1"));
        executorService.execute(new FileThread(2, "线程2"));
        executorService.execute(new FileThread(3, "线程3"));
        executorService.execute(new FileThread(4, "线程4"));
        executorService.shutdown();
    }

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            writeToFile(value);
        }
    }

    private void writeToFile(int value) {
        //TODO:
        System.out.println("Thread " + name + " Write to file value:" + value);
    }

    public int getValue() {
        return value;
    }

    public String getName() {
        return name;
    }
}
